<!DOCTYPE html>
<!-- saved from url=(0049)http://www.cnblogs.com/dolphin0520/p/3799052.html -->
<html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script async="" src="./Java中的static关键字解析 - 海 子 - 博客园_files/analytics.js"></script>
<meta charset="utf-8">
<title>Java中的static关键字解析 - 海 子 - 博客园</title>
<link type="text/css" rel="stylesheet" href="./Java中的static关键字解析 - 海 子 - 博客园_files/blog-common.css">
<link id="MainCss" type="text/css" rel="stylesheet" href="./Java中的static关键字解析 - 海 子 - 博客园_files/bundle-darkgreentrip.css">
<link type="text/css" rel="stylesheet" href="./Java中的static关键字解析 - 海 子 - 博客园_files/86633.css">
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/dolphin0520/rss">
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/dolphin0520/rsd.xml">
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/dolphin0520/wlwmanifest.xml">
<script type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/encoder.js"></script><script async="" type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/gpt.js"></script><script src="./Java中的static关键字解析 - 海 子 - 博客园_files/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'dolphin0520', cb_enable_mathjax=true;</script>
<script src="./Java中的static关键字解析 - 海 子 - 博客园_files/blog-common.js" type="text/javascript"></script>
<script type="text/x-mathjax-config;executed=true">MathJax.Hub.Config({
  tex2jax: { inlineMath: [['$','$'], ['\\(','\\)']] },
  TeX: { equationNumbers: { autoNumber: ['AMS'], useLabelIds: true } },
  'HTML-CSS': { linebreaks: { automatic: true } },
  SVG: { linebreaks: { automatic: true } }});</script><script type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/MathJax.js"></script><script async="" type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/pubads_impl_65.js"></script><style type="text/css">.MathJax_Hover_Frame {border-radius: .25em; -webkit-border-radius: .25em; -moz-border-radius: .25em; -khtml-border-radius: .25em; box-shadow: 0px 0px 15px #83A; -webkit-box-shadow: 0px 0px 15px #83A; -moz-box-shadow: 0px 0px 15px #83A; -khtml-box-shadow: 0px 0px 15px #83A; border: 1px solid #A6D ! important; display: inline-block; position: absolute}
.MathJax_Hover_Arrow {position: absolute; width: 15px; height: 11px; cursor: pointer}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; color: black; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuArrow.RTL {left: .5em; right: auto}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuCheck.RTL {right: .7em; left: auto}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck.RTL {right: .7em; left: auto}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
.MathJax_Menu_Close {position: absolute; width: 31px; height: 31px; top: -15px; left: -15px}
</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
#MathJax_ZoomFrame {position: relative; display: inline-block; height: 0; width: 0}
#MathJax_ZoomEventTrap {position: absolute; left: 0; top: 0; z-index: 302; display: inline-block; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">.MathJax_Preview {color: #888}
#MathJax_Message {position: fixed; left: 1em; bottom: 1.5em; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><script type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/osd.js"></script></head>
<body><div id="MathJax_Message" style="display: none;"></div>
<a name="top"></a>

<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="http://www.cnblogs.com/dolphin0520/"><img id="blogLogo" src="./Java中的static关键字解析 - 海 子 - 博客园_files/logo.gif" alt="返回主页"></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/dolphin0520/">海 子</a></h1>
<h2>不经历风雨，怎能见彩虹？做一个快乐的程序员。</h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/dolphin0520/">首页</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/%E6%B5%B7%20%E5%AD%90">联系</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/dolphin0520/rss">订阅</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/dolphin0520/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li>
</ul>
		<div class="blogStats">
			
			
<!--done-->
随笔- 184&nbsp;
文章- 0&nbsp;
评论- 668&nbsp;

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		
<div id="post_detail">
<!--done-->
<div id="topics">
	<div class="post">
		<h1 class="postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="./Java中的static关键字解析 - 海 子 - 博客园_files/Java中的static关键字解析 - 海 子 - 博客园.html">Java中的static关键字解析</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body"><p style="text-align: center;">Java中的static关键字解析</p>
<p style="text-align: left;">　　static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字，也是各大公司的面试官喜欢在面试时问到的知识点之一。下面就先讲述一下static关键字的用法和平常容易误解的地方，最后列举了一些面试笔试中常见的关于static的考题。以下是本文的目录大纲：</p>
<p style="text-align: left;">　　一.static关键字的用途</p>
<p style="text-align: left;">　　二.static关键字的误区</p>
<p style="text-align: left;">　　三.常见的笔试面试题</p>
<p style="text-align: left;">　　若有不正之处，希望谅解并欢迎批评指正。</p>
<p style="text-align: left;">　　请尊重作者劳动成果，转载请标明原文链接：</p>
<p style="text-align: left;">　　<a href="./Java中的static关键字解析 - 海 子 - 博客园_files/Java中的static关键字解析 - 海 子 - 博客园.html">http://www.cnblogs.com/dolphin0520/p/3799052.html</a></p>
<h2 style="text-align: left;">一.static关键字的用途</h2>
<p style="text-align: left;">　　在《Java编程思想》P86页有这样一段话：</p>
<p style="text-align: left;">　　<span style="color: #0000ff;">“static方法就是没有this的方法。在static方法内部不能调用非静态方法，反过来是可以的。而且可以在没有创建任何对象的前提下，仅仅通过类本身来调用static方法。这实际上正是static方法的主要用途。”</span></p>
<p style="text-align: left;"><span style="color: #0000ff;">　　<span style="color: #000000;">这段话虽然只是说明了static方法的特殊之处，但是可以看出static关键字的基本作用，简而言之，一句话来描述就是：</span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;">　　<span style="color: #ff0000;">方便在没有创建对象的情况下来进行调用（方法/变量）。</span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;">　　<span style="color: #000000;">很显然，被static关键字修饰的方法或者变量不需要依赖于对象来进行访问，只要类被加载了，就可以通过类名去进行访问。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　static可以用来修饰类的成员方法、类的成员变量，另外可以编写static代码块来优化程序性能。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">1）static方法</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　static方法一般称作静态方法，由于静态方法不依赖于任何对象就可以进行访问，因此对于静态方法来说，是没有this的，因为它不依附于任何对象，既然都没有对象，就谈不上this了。并且由于这个特性，在静态方法中不能访问类的非静态成员变量和非静态成员方法，因为非静态成员方法/变量都是必须依赖具体的对象才能够被调用。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　但是要注意的是，虽然在静态方法中不能访问非静态成员方法和非静态成员变量，但是在非静态成员方法中是可以访问静态成员方法/变量的。举个简单的例子：</span></span></span></span></p>
<p style="text-align: left;"><img src="./Java中的static关键字解析 - 海 子 - 博客园_files/201612439117678.jpg" alt=""></p>
<p style="text-align: left;">　　在上面的代码中，由于print2方法是独立于对象存在的，可以直接用过类名调用。假如说可以在静态方法中访问非静态方法/变量的话，那么如果在main方法中有下面一条语句：</p>
<p style="text-align: left;">　　MyObject.print2();</p>
<p style="text-align: left;">　　此时对象都没有，str2根本就不存在，所以就会产生矛盾了。同样对于方法也是一样，由于你无法预知在print1方法中是否访问了非静态成员变量，所以也禁止在静态成员方法中访问非静态成员方法。</p>
<p style="text-align: left;">　　而对于非静态成员方法，它访问静态成员方法/变量显然是毫无限制的。</p>
<p style="text-align: left;">　　因此，如果说想在不创建对象的情况下调用某个方法，就可以将这个方法设置为static。我们最常见的static方法就是main方法，至于为什么main方法必须是static的，现在就很清楚了。因为程序在执行main方法的时候没有创建任何对象，因此只有通过类名来访问。</p>
<p style="text-align: left;">　　<span style="color: #ff0000;">另外记住，即使没有显示地声明为static，类的构造器实际上也是静态方法</span>。</p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">2）static变量</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　static变量也称作静态变量，静态变量和非静态变量的区别是：静态变量被所有的对象所共享，在内存中只有一个副本，它当且仅当在类初次加载时会被初始化。而非静态变量是对象所拥有的，在创建对象的时候被初始化，存在多个副本，各个对象拥有的副本互不影响。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　static成员变量的初始化顺序按照定义的顺序进行初始化。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">3）static代码块</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　static关键字还有一个比较关键的作用就是 用来形成静态代码块以优化程序性能。static块可以置于类中的任何地方，类中可以有多个static块。在类初次被加载的时候，会按照static块的顺序来执行每个static块，并且只会执行一次。</span></span></span></span></p>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　为什么说static块可以用来优化程序性能，是因为它的特性:只会在类加载的时候执行一次。下面看个例子:</span></span></span></span></p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_838813" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">class</code> <code class="java plain">Person{</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">Date birthDate;</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Person(Date birthDate) {</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">this</code><code class="java plain">.birthDate = birthDate;</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">boolean</code> <code class="java plain">isBornBoomer() {</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Date startDate = Date.valueOf(</code><code class="java string">"1946"</code><code class="java plain">);</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Date endDate = Date.valueOf(</code><code class="java string">"1964"</code><code class="java plain">);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">birthDate.compareTo(startDate)&gt;=</code><code class="java value">0</code> <code class="java plain">&amp;&amp; birthDate.compareTo(endDate) &lt; </code><code class="java value">0</code><code class="java plain">;</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number13 index12 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　isBornBoomer是用来这个人是否是1946-1964年出生的，而每次isBornBoomer被调用的时候，都会生成startDate和birthDate两个对象，造成了空间浪费，如果改成这样效率会更好：</span></span></span></span></p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_782067" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">class</code> <code class="java plain">Person{</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java plain">Date birthDate;</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java keyword">static</code> <code class="java plain">Date startDate,endDate;</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">startDate = Date.valueOf(</code><code class="java string">"1946"</code><code class="java plain">);</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">endDate = Date.valueOf(</code><code class="java string">"1964"</code><code class="java plain">);</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Person(Date birthDate) {</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">this</code><code class="java plain">.birthDate = birthDate;</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">boolean</code> <code class="java plain">isBornBoomer() {</code></div><div class="line number14 index13 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">return</code> <code class="java plain">birthDate.compareTo(startDate)&gt;=</code><code class="java value">0</code> <code class="java plain">&amp;&amp; birthDate.compareTo(endDate) &lt; </code><code class="java value">0</code><code class="java plain">;</code></div><div class="line number15 index14 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number16 index15 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p style="text-align: left;"><span style="color: #0000ff;"><span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #000000;">　　因此，很多时候会将一些只需要进行一次的初始化操作都放在static代码块中进行。</span></span></span></span></p>
<h2 style="text-align: left;">二.static关键字的误区</h2>
<p style="text-align: left;">1.static关键字会改变类中成员的访问权限吗？</p>
<p style="text-align: left;">　　有些初学的朋友会将java中的static与C/C++中的static关键字的功能混淆了。在这里只需要记住一点：与C/C++中的static不同，Java中的static关键字不会影响到变量或者方法的作用域。在Java中能够影响到访问权限的只有private、public、protected（包括包访问权限）这几个关键字。看下面的例子就明白了：</p>
<p style="text-align: left;"><img src="./Java中的static关键字解析 - 海 子 - 博客园_files/201658474265949.jpg" alt=""></p>
<p style="text-align: left;">　　提示错误"Person.age 不可视"，这说明static关键字并不会改变变量和方法的访问权限。</p>
<p style="text-align: left;">2.能通过this访问静态成员变量吗？</p>
<p style="text-align: left;">　　虽然对于静态方法来说没有this，那么在非静态方法中能够通过this访问静态成员变量吗？先看下面的一个例子，这段代码输出的结果是什么？</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_624593" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">Main {　　</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code> <code class="java keyword">int</code> <code class="java plain">value = </code><code class="java value">33</code><code class="java plain">; </code></div><div class="line number3 index2 alt2">&nbsp;</div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">main(String[] args) </code><code class="java keyword">throws</code> <code class="java plain">Exception{</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">new</code> <code class="java plain">Main().printValue();</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number7 index6 alt2">&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">private</code> <code class="java keyword">void</code> <code class="java plain">printValue(){</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">int</code> <code class="java plain">value = </code><code class="java value">3</code><code class="java plain">;</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java keyword">this</code><code class="java plain">.value);</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number12 index11 alt1"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code" onclick="cnblogs_code_show(&#39;38ca690a-adb8-4228-9c54-c09f295f6945&#39;)"><img id="code_img_closed_38ca690a-adb8-4228-9c54-c09f295f6945" class="code_img_closed" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ContractedBlock.gif" alt=""><img id="code_img_opened_38ca690a-adb8-4228-9c54-c09f295f6945" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide(&#39;38ca690a-adb8-4228-9c54-c09f295f6945&#39;,event)" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_38ca690a-adb8-4228-9c54-c09f295f6945" class="cnblogs_code_hide">
<pre>33</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>　　这里面主要考察队this和static的理解。this代表什么？this代表当前对象，那么通过new Main()来调用printValue的话，当前对象就是通过new Main()生成的对象。而static变量是被对象所享有的，因此在printValue中的this.value的值毫无疑问是33。在printValue方法内部的value是局部变量，根本不可能与this关联，所以输出结果是33。在这里永远要记住一点：静态成员变量虽然独立于对象，但是不代表不可以通过对象去访问，所有的静态方法和静态变量都可以通过对象访问（只要访问权限足够）。</p>
<p style="text-align: left;">3.static能作用于局部变量么？</p>
<p style="text-align: left;">　　在C/C++中static是可以作用域局部变量的，但是在Java中切记：<span style="color: #ff0000;">static是不允许用来修饰局部变量</span>。不要问为什么，这是Java语法的规定。</p>
<h2 style="text-align: left;">三.常见的笔试面试题</h2>
<p style="text-align: left;">　　下面列举一些面试笔试中经常遇到的关于static关键字的题目，仅供参考，如有补充欢迎下方留言。</p>
<p style="text-align: left;">1.下面这段代码的输出结果是什么？</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_769238" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">Test </code><code class="java keyword">extends</code> <code class="java plain">Base{</code></div><div class="line number2 index1 alt1">&nbsp;</div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test static"</code><code class="java plain">);</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Test(){</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test constructor"</code><code class="java plain">);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">main(String[] args) {</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">new</code> <code class="java plain">Test();</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number14 index13 alt1"><code class="java plain">}</code></div><div class="line number15 index14 alt2">&nbsp;</div><div class="line number16 index15 alt1"><code class="java keyword">class</code> <code class="java plain">Base{</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"base static"</code><code class="java plain">);</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Base(){</code></div><div class="line number23 index22 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"base constructor"</code><code class="java plain">);</code></div><div class="line number24 index23 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number25 index24 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<div class="cnblogs_code" onclick="cnblogs_code_show(&#39;2b43f033-e9b4-4aa2-9cc7-232c4a3bff0d&#39;)"><img id="code_img_closed_2b43f033-e9b4-4aa2-9cc7-232c4a3bff0d" class="code_img_closed" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ContractedBlock.gif" alt="" style="display: none;"><img id="code_img_opened_2b43f033-e9b4-4aa2-9cc7-232c4a3bff0d" class="code_img_opened" style="display: inline;" onclick="cnblogs_code_hide(&#39;2b43f033-e9b4-4aa2-9cc7-232c4a3bff0d&#39;,event)" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_2b43f033-e9b4-4aa2-9cc7-232c4a3bff0d" class="cnblogs_code_hide" style="display: block;">
<pre>base <span style="color: #0000ff;">static</span><span style="color: #000000;">
test </span><span style="color: #0000ff;">static</span><span style="color: #000000;">
base constructor
test constructor</span></pre>
</div>
<span class="cnblogs_code_collapse" style="display: none;">View Code</span></div>
<p>　　至于为什么是这个结果，我们先不讨论，先来想一下这段代码具体的执行过程，在执行开始，先要寻找到main方法，因为main方法是程序的入口，但是在执行main方法之前，必须先加载Test类，而在加载Test类的时候发现Test类继承自Base类，因此会转去先加载Base类，在加载Base类的时候，发现有static块，便执行了static块。在Base类加载完成之后，便继续加载Test类，然后发现Test类中也有static块，便执行static块。在加载完所需的类之后，便开始执行main方法。在main方法中执行new Test()的时候会先调用父类的构造器，然后再调用自身的构造器。因此，便出现了上面的输出结果。</p>
<p>2.这段代码的输出结果是什么？</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_213395" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">Test {</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Person person = </code><code class="java keyword">new</code> <code class="java plain">Person(</code><code class="java string">"Test"</code><code class="java plain">);</code></div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test static"</code><code class="java plain">);</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Test() {</code></div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test constructor"</code><code class="java plain">);</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">main(String[] args) {</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">new</code> <code class="java plain">MyClass();</code></div><div class="line number13 index12 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number14 index13 alt1"><code class="java plain">}</code></div><div class="line number15 index14 alt2">&nbsp;</div><div class="line number16 index15 alt1"><code class="java keyword">class</code> <code class="java plain">Person{</code></div><div class="line number17 index16 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number18 index17 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"person static"</code><code class="java plain">);</code></div><div class="line number19 index18 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number20 index19 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">Person(String str) {</code></div><div class="line number21 index20 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"person "</code><code class="java plain">+str);</code></div><div class="line number22 index21 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number23 index22 alt2"><code class="java plain">}</code></div><div class="line number24 index23 alt1">&nbsp;</div><div class="line number25 index24 alt2">&nbsp;</div><div class="line number26 index25 alt1"><code class="java keyword">class</code> <code class="java plain">MyClass </code><code class="java keyword">extends</code> <code class="java plain">Test {</code></div><div class="line number27 index26 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">Person person = </code><code class="java keyword">new</code> <code class="java plain">Person(</code><code class="java string">"MyClass"</code><code class="java plain">);</code></div><div class="line number28 index27 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number29 index28 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"myclass static"</code><code class="java plain">);</code></div><div class="line number30 index29 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number31 index30 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number32 index31 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java plain">MyClass() {</code></div><div class="line number33 index32 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"myclass constructor"</code><code class="java plain">);</code></div><div class="line number34 index33 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number35 index34 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<div class="cnblogs_code" onclick="cnblogs_code_show(&#39;6b107849-8aa0-4b18-ad32-237c7e64a2fc&#39;)"><img id="code_img_closed_6b107849-8aa0-4b18-ad32-237c7e64a2fc" class="code_img_closed" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ContractedBlock.gif" alt=""><img id="code_img_opened_6b107849-8aa0-4b18-ad32-237c7e64a2fc" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide(&#39;6b107849-8aa0-4b18-ad32-237c7e64a2fc&#39;,event)" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_6b107849-8aa0-4b18-ad32-237c7e64a2fc" class="cnblogs_code_hide">
<pre>test <span style="color: #0000ff;">static</span><span style="color: #000000;">
myclass </span><span style="color: #0000ff;">static</span><span style="color: #000000;">
person </span><span style="color: #0000ff;">static</span><span style="color: #000000;">
person Test
test constructor
person MyClass
myclass constructor</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>　　类似地，我们还是来想一下这段代码的具体执行过程。首先加载Test类，因此会执行Test类中的static块。接着执行new MyClass()，而MyClass类还没有被加载，因此需要加载MyClass类。在加载MyClass类的时候，发现MyClass类继承自Test类，但是由于Test类已经被加载了，所以只需要加载MyClass类，那么就会执行MyClass类的中的static块。在加载完之后，就通过构造器来生成对象。而在生成对象的时候，必须先初始化父类的成员变量，因此会执行Test中的Person person = new Person()，而Person类还没有被加载过，因此会先加载Person类并执行Person类中的static块，接着执行父类的构造器，完成了父类的初始化，然后就来初始化自身了，因此会接着执行MyClass中的Person person = new Person()，最后执行MyClass的构造器。</p>
<p>3.这段代码的输出结果是什么？</p>
<div class="cnblogs_Highlighter sh-gutter">
<div><div id="highlighter_869018" class="syntaxhighlighter  java"><div class="toolbar"><span><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" class="toolbar_item command_help help">?</a></span></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">Test {</code></div><div class="line number2 index1 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number3 index2 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number4 index3 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test static 1"</code><code class="java plain">);</code></div><div class="line number5 index4 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number6 index5 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">public</code> <code class="java keyword">static</code> <code class="java keyword">void</code> <code class="java plain">main(String[] args) {</code></div><div class="line number7 index6 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number8 index7 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number9 index8 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code>&nbsp;</div><div class="line number10 index9 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java keyword">static</code><code class="java plain">{</code></div><div class="line number11 index10 alt2"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">System.out.println(</code><code class="java string">"test static 2"</code><code class="java plain">);</code></div><div class="line number12 index11 alt1"><code class="java spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="java plain">}</code></div><div class="line number13 index12 alt2"><code class="java plain">}</code></div></div></td></tr></tbody></table></div></div>
</div>
<div class="cnblogs_code" onclick="cnblogs_code_show(&#39;ef1ef77f-76ed-43b5-b4ac-2db83bc9052a&#39;)"><img id="code_img_closed_ef1ef77f-76ed-43b5-b4ac-2db83bc9052a" class="code_img_closed" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ContractedBlock.gif" alt=""><img id="code_img_opened_ef1ef77f-76ed-43b5-b4ac-2db83bc9052a" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide(&#39;ef1ef77f-76ed-43b5-b4ac-2db83bc9052a&#39;,event)" src="./Java中的static关键字解析 - 海 子 - 博客园_files/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_ef1ef77f-76ed-43b5-b4ac-2db83bc9052a" class="cnblogs_code_hide">
<pre>test <span style="color: #0000ff;">static</span> 1<span style="color: #000000;">
test </span><span style="color: #0000ff;">static</span> 2</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>　　虽然在main方法中没有任何语句，但是还是会输出，原因上面已经讲述过了。另外，static块可以出现类中的任何地方（只要不是方法内部，记住，任何方法内部都不行），并且执行是按照static块的顺序执行的。</p>
<p style="text-align: left;">　　参考资料：</p>
<p style="text-align: left;">　　<a href="http://lavasoft.blog.51cto.com/62575/18771/" target="_blank">http://lavasoft.blog.51cto.com/62575/18771/</a></p>
<p style="text-align: left;">　　<a href="http://www.51cto.com/specbook/24/35011.htm" target="_blank">http://www.51cto.com/specbook/24/35011.htm</a></p>
<p style="text-align: left;">　　<a href="http://blog.csdn.net/zhu_apollo/article/details/1888219" target="_blank">http://blog.csdn.net/zhu_apollo/article/details/1888219</a></p>
<p style="text-align: left;">　　<a href="http://blog.sina.com.cn/s/blog_70b845780100n9zz.html" target="_blank">http://blog.sina.com.cn/s/blog_70b845780100n9zz.html</a></p>
<p style="text-align: left;">　　<a href="http://hi.baidu.com/yuiezt/item/b71ff5fbfe9c385cc8f3370d" target="_blank">http://hi.baidu.com/yuiezt/item/b71ff5fbfe9c385cc8f3370d</a></p>
<p style="text-align: left;">　　<a href="http://bbs.csdn.net/topics/330251070" target="_blank">http://bbs.csdn.net/topics/330251070</a></p>
<p style="text-align: left;">　　<a href="http://yezixingchen.iteye.com/blog/1597186" target="_blank">http://yezixingchen.iteye.com/blog/1597186</a></p>
<p style="text-align: left;">　　《Java编程思想》</p></div><div id="MySignature" style="display: block;"><div>作者：<a href="http://www.cnblogs.com/dolphin0520/" target="_blank">海子</a></div>
　　　　<div>出处：<a href="http://www.cnblogs.com/dolphin0520/" target="_blank">http://www.cnblogs.com/dolphin0520/</a></div>
　　　　<div>本博客中未标明转载的文章归作者<a href="http://www.cnblogs.com/dolphin0520/" target="_blank">海子</a>和博客园共有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。 
</div>
</div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory">分类: <a href="http://www.cnblogs.com/dolphin0520/category/361055.html">Java基础</a></div>
<div id="EntryTag">标签: <a href="http://www.cnblogs.com/dolphin0520/tag/Java/">Java</a></div>
<div id="blog_post_info"><div id="green_channel">
绿色通道：
<a href="javascript:void(0);" id="green_channel_digg" onclick="DiggIt(3799052,cb_blogId,1);green_channel_success(this,&#39;谢谢推荐！&#39;);">好文要顶</a>
<a id="green_channel_follow" onclick="c_follow();" href="javascript:void(0);">关注我</a>
<a id="green_channel_favorite" onclick="AddToWz(cb_entryId);return false;" href="javascript:void(0);">收藏该文</a><a id="green_channel_contact" href="http://msg.cnblogs.com/send/%E6%B5%B7%20%E5%AD%90" target="_blank">与我联系</a>
<a id="green_channel_weibo" href="javascript:void(0);" title="分享至新浪微博" onclick="ShareToTsina()"><img src="./Java中的static关键字解析 - 海 子 - 博客园_files/icon_weibo_24.png" alt=""></a>
</div>
<div id="author_profile">
<div id="author_profile_info" class="author_profile_info">
<a href="http://home.cnblogs.com/u/dolphin0520/" target="_blank"><img src="./Java中的static关键字解析 - 海 子 - 博客园_files/u288799.jpg" class="author_avatar" alt=""></a>
<div id="author_profile_detail" class="author_profile_info">
<a href="http://home.cnblogs.com/u/dolphin0520/">海 子</a><br>
<a href="http://home.cnblogs.com/u/dolphin0520/followees">关注 - 6</a><br>
<a href="http://home.cnblogs.com/u/dolphin0520/followers">粉丝 - 1041</a>
</div>
</div>
<div class="clear"></div>
<div id="author_profile_honor"></div>
<div id="author_profile_follow">
    <a href="javascript:void(0);" onclick="c_follow();return false;">+加关注</a>
</div>
</div>
<div id="div_digg">										
    <div class="diggit" onclick="votePost(3799052,&#39;Digg&#39;)">
        <span class="diggnum" id="digg_count">7</span>
    </div>
	<div class="buryit" onclick="votePost(3799052,&#39;Bury&#39;)"> 
		<span class="burynum" id="bury_count">0</span>
	</div>
	<div class="clear"></div>	
	<div class="diggword" id="digg_tips">
    (请您对文章做出评价)
    </div>	
</div>
</div>
<div class="clear"></div>
<div id="post_next_prev"><a href="http://www.cnblogs.com/dolphin0520/p/3783345.html" class="p_n_p_prefix">« </a> 上一篇：<a href="http://www.cnblogs.com/dolphin0520/p/3783345.html" title="发布于2014-06-18 15:28">Java垃圾回收机制</a><br><a href="http://www.cnblogs.com/dolphin0520/p/3803432.html" class="p_n_p_prefix">» </a> 下一篇：<a href="http://www.cnblogs.com/dolphin0520/p/3803432.html" title="发布于2014-06-26 17:29">Java：类与继承</a><br></div>
</div>


		</div>
		<div class="postDesc">posted @ <span id="post-date">2014-06-20 17:47</span> <a href="http://www.cnblogs.com/dolphin0520/">海 子</a> 阅读(<span id="post_view_count">12331</span>) 评论(<span id="post_comment_count">6</span>)  <a href="http://i.cnblogs.com/EditPosts.aspx?postid=3799052" rel="nofollow">编辑</a> <a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" onclick="AddToWz(3799052);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=86633,cb_entryId=3799052,cb_blogApp=currentBlogApp,cb_blogUserGuid='ab3961d0-cd5d-e011-a53f-842b2b196315',cb_entryCreatedDate='2014/6/20 17:47:00';loadViewCount(cb_entryId);</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"><div id="comments_pager_top"></div>
<br>
<div class="feedback_area_title">评论列表</div>
<div class="feedbackNoItems"></div>	

		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3054377" class="layer">#1楼</a><a name="3054377" id="comment_anchor_3054377"></a>  <span class="comment_date">2014-10-30 17:57</span> <a id="a_comment_author_3054377" href="http://www.cnblogs.com/valentino24/" target="_blank">Mayboy</a> <a href="http://msg.cnblogs.com/send/Mayboy" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3054377" class="blog_comment_body">构造方法不是类方法，因为可以在构造方法中使用this关键字</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3054377,&#39;Digg&#39;,this)">支持(1)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3054377,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_3054377_avatar" style="display:none;">http://pic.cnblogs.com/face/532579/20141018172713.png</span>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3054620" class="layer">#2楼</a><a name="3054620" id="comment_anchor_3054620"></a>  <span class="comment_date">2014-10-31 08:35</span> <a id="a_comment_author_3054620" href="http://www.cnblogs.com/valentino24/" target="_blank">Mayboy</a> <a href="http://msg.cnblogs.com/send/Mayboy" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3054620" class="blog_comment_body">“……而每次isBornBoomer被调用的时候，都会生成startDate和birthDate两个对象，……”是startDate和endDate</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3054620,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3054620,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_3054620_avatar" style="display:none;">http://pic.cnblogs.com/face/532579/20141018172713.png</span>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3185176" class="layer">#3楼</a><a name="3185176" id="comment_anchor_3185176"></a>  <span class="comment_date">2015-05-16 10:29</span> <a id="a_comment_author_3185176" href="http://www.cnblogs.com/jaxer/" target="_blank">o对酒当歌</a> <a href="http://msg.cnblogs.com/send/o%E5%AF%B9%E9%85%92%E5%BD%93%E6%AD%8C" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3185176" class="blog_comment_body">主要来看static代码块的，不错，学习了！</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3185176,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3185176,&#39;Bury&#39;,this)">反对(0)</a></div><span id="comment_3185176_avatar" style="display:none;">http://pic.cnblogs.com/face/704364/20150511165024.png</span>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3199570" class="layer">#4楼</a><a name="3199570" id="comment_anchor_3199570"></a>  <span class="comment_date">2015-06-03 18:15</span> <a id="a_comment_author_3199570" href="http://www.cnblogs.com/nb44c/" target="_blank">nb44c</a> <a href="http://msg.cnblogs.com/send/nb44c" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3199570" class="blog_comment_body">楼主说的这个构造方法是static的这个好像不对吧</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3199570,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3199570,&#39;Bury&#39;,this)">反对(0)</a></div>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3200486" class="layer">#5楼</a><a name="3200486" id="comment_anchor_3200486"></a>  <span class="comment_date">2015-06-04 16:01</span> <a id="a_comment_author_3200486" href="http://home.cnblogs.com/u/765693/" target="_blank">宋小俊要加油</a> <a href="http://msg.cnblogs.com/send/%E5%AE%8B%E5%B0%8F%E4%BF%8A%E8%A6%81%E5%8A%A0%E6%B2%B9" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3200486" class="blog_comment_body">很好</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3200486,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3200486,&#39;Bury&#39;,this)">反对(0)</a></div>
			</div>
		</div>
	
		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<span class="comment_actions"></span>
				</div>
				<a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#3203009" class="layer">#6楼</a><a name="3203009" id="comment_anchor_3203009"></a><span id="comment-maxId" style="display:none;">3203009</span><span id="comment-maxDate" style="display:none;">2015/6/7 18:22:01</span>  <span class="comment_date">2015-06-07 18:22</span> <a id="a_comment_author_3203009" href="http://home.cnblogs.com/u/771713/" target="_blank">nonefly</a> <a href="http://msg.cnblogs.com/send/nonefly" title="发送站内短消息" class="sendMsg2This">&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<div id="comment_body_3203009" class="blog_comment_body">第二句红字错了吧？构造器怎么可能是static？static修饰的会在加载时执行，而构造器这时候还没出生呢，构造器在new了对象后才会执行去初始化值。</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3203009,&#39;Digg&#39;,this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3203009,&#39;Bury&#39;,this)">反对(0)</a></div>
			</div>
		</div>
	<div id="comments_pager_bottom"></div></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id="comment_form" class="commentform">
<a name="commentform"></a>
<div id="divCommentShow"></div>
<div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" id="lnk_RefreshComments" onclick="return RefreshCommentList();">刷新评论</a><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#" onclick="return RefreshPage();">刷新页面</a><a href="http://www.cnblogs.com/dolphin0520/p/3799052.html#top">返回顶部</a></div>
<div id="comment_form_container"><div class="login_tips">注册用户登录后才能发表评论，请 <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return login(&#39;commentform&#39;);">登录</a> 或 <a rel="nofollow" href="javascript:void(0);" class="underline" onclick="return register();">注册</a>，<a href="http://www.cnblogs.com/">访问</a>网站首页。</div></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
<div id="site_nav_under"><a href="http://www.ucancode.com/index.htm" target="_blank">【推荐】50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库</a><br><a href="http://www.gcpowertools.com.cn/products/spreadstudio/excel_like.htm?utm_source=cnblogs&utm_medium=blogpage&utm_term=SpreadStudio&utm_content=ExcelLike&utm_campaign=community" target="_blank">【推荐】如何让你的程序拥有象Excel一样强大的数据编辑功能</a><br></div>
<div id="opt_under_post"></div>
<script type="text/javascript">
    var enableGoogleAd = canShowAdsense(); 
    fixPostBodyFormat();
</script>
<script type="text/javascript">
  var googletag = googletag || {};
  googletag.cmd = googletag.cmd || [];
    (function () {
        if (enableGoogleAd) {
            var gads = document.createElement('script');
            gads.async = true;
            gads.type = 'text/javascript';
            var useSSL = 'https:' == document.location.protocol;
            gads.src = (useSSL ? 'https:' : 'http:') +
              '//www.googletagservices.com/tag/js/gpt.js';
            var node = document.getElementsByTagName('script')[0];
            node.parentNode.insertBefore(gads, node);
        }
  })();
</script>
<script type="text/javascript">
    try {
        if (enableGoogleAd) {
            googletag.cmd.push(function() {
            googletag.defineSlot('/1090369/cnblogs_blogpost_C2', [468, 60], 'div-gpt-ad-1433581717989-0').addService(googletag.pubads());
            googletag.defineSlot('/1090369/cnblogs_blogpost_C1_sitehome', [300, 250], 'div-gpt-ad-1433581717989-1').addService(googletag.pubads());
            googletag.pubads().enableSingleRequest();
            googletag.pubads().collapseEmptyDivs();
            googletag.enableServices();
            });
        };
    } catch (e) { }
</script>
<div id="google_ad_c1" class="c_ad_block">
    <div id="div-gpt-ad-1433581717989-1" style="height:250px; width:300px;">
    <script type="text/javascript">
        try {
            if (enableGoogleAd) {
                googletag.cmd.push(function () { googletag.display('div-gpt-ad-1433581717989-1'); });
            } else {
                $('#div-gpt-ad-1433581717989-1').hide();
            }
    } catch (e) { }
    </script>
    <div id="google_ads_iframe_/1090369/cnblogs_blogpost_C1_sitehome_0__container__" style="border: 0pt none;"><iframe id="google_ads_iframe_/1090369/cnblogs_blogpost_C1_sitehome_0" name="google_ads_iframe_/1090369/cnblogs_blogpost_C1_sitehome_0" width="300" height="250" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" src="javascript:"<html><body style='background:transparent'></body></html>"" style="border: 0px; vertical-align: bottom;"></iframe></div><iframe id="google_ads_iframe_/1090369/cnblogs_blogpost_C1_sitehome_0__hidden__" name="google_ads_iframe_/1090369/cnblogs_blogpost_C1_sitehome_0__hidden__" width="0" height="0" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" src="javascript:"<html><body style='background:transparent'></body></html>"" style="border: 0px; vertical-align: bottom; visibility: hidden; display: none;"></iframe></div>
</div>
<div id="under_post_news"><div class="itnews c_ad_block"><b>最新IT新闻</b>:<br> ·  <a href="http://news.cnblogs.com/n/524453/" target="_blank">观点：希腊经济危机凸显云服务对创业生态系统的重要性</a><br> ·  <a href="http://news.cnblogs.com/n/524452/" target="_blank">哈佛大学IT系统6月出现漏洞入侵，影响8个学院和行政机构</a><br> ·  <a href="http://news.cnblogs.com/n/524451/" target="_blank">滴滴打车发“合乘拼车”系统  将在全平台上线</a><br> ·  <a href="http://news.cnblogs.com/n/524450/" target="_blank">教育巨头陷互联网价格战：新东方在线血拼1元课</a><br> ·  <a href="http://news.cnblogs.com/n/524449/" target="_blank">51创始人庞升东：创业14年的三条核心总结</a><br>» <a href="http://news.cnblogs.com/" title="IT新闻" target="_blank">更多新闻...</a></div></div>
<div id="google_ad_c2" class="c_ad_block">
<div id="div-gpt-ad-1433581717989-0" style="height:60px; width:468px;">
<script type="text/javascript">
try {
    if (enableGoogleAd) {
        googletag.cmd.push(function () { googletag.display('div-gpt-ad-1433581717989-0'); });
    } else {
        $('#div-gpt-ad-1433581717989-0').hide();
    }
} catch (e) { }
</script>
<div id="google_ads_iframe_/1090369/cnblogs_blogpost_C2_0__container__" style="border: 0pt none;"><iframe id="google_ads_iframe_/1090369/cnblogs_blogpost_C2_0" name="google_ads_iframe_/1090369/cnblogs_blogpost_C2_0" width="468" height="60" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" src="javascript:"<html><body style='background:transparent'></body></html>"" style="border: 0px; vertical-align: bottom;"></iframe></div></div>
</div>
<div id="under_post_kb"><div class="itnews c_ad_block" id="kb_block"><b>最新知识库文章</b>:<br><div id="kb_recent"> ·  <a href="http://kb.cnblogs.com/page/523508/" target="_blank">所谓软件工程</a><br> ·  <a href="http://kb.cnblogs.com/page/519939/" target="_blank">也谈怎么学好英语这件小事</a><br> ·  <a href="http://kb.cnblogs.com/page/524041/" target="_blank">前后端分离了，然后呢？</a><br> ·  <a href="http://kb.cnblogs.com/page/521718/" target="_blank">撰写合格的REST API</a><br> ·  <a href="http://kb.cnblogs.com/page/523446/" target="_blank">深入NGINX：我们如何设计它的性能和扩展性</a><br></div>» <a href="http://kb.cnblogs.com/" target="_blank">更多知识库文章...</a></div></div>
<div id="HistoryToday" class="c_ad_block"></div>
<script type="text/javascript">
$(function () {
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
    setTimeout(function () { incrementViewCount(cb_entryId); }, 200);
});
</script>
</div>

	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"><div id="profile_block">昵称：<a href="http://home.cnblogs.com/u/dolphin0520/">海 子</a><br>园龄：<a href="http://home.cnblogs.com/u/dolphin0520/" title="入园时间：2011-04-03">4年3个月</a><br>粉丝：<a href="http://home.cnblogs.com/u/dolphin0520/followers/">1041</a><br>关注：<a href="http://home.cnblogs.com/u/dolphin0520/followees/">6</a><div id="p_b_follow"><a href="javascript:void(0);" onclick="cnblogs.UserManager.FollowBlogger(&#39;ab3961d0-cd5d-e011-a53f-842b2b196315&#39;)">+加关注</a></div></div></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="blog-calendar" style=""><table id="blogCalendar" class="Cal" cellspacing="0" cellpadding="0" title="日历">
	<tbody><tr><td colspan="7"><table class="CalTitle" cellspacing="0">
		<tbody><tr><td class="CalNextPrev"><a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2015/06/01&#39;);return false;">&lt;</a></td><td align="center">2015年7月</td><td class="CalNextPrev" align="right"><a href="javascript:void(0);" onclick="loadBlogCalendar(&#39;2015/08/01&#39;);return false;">&gt;</a></td></tr>
	</tbody></table></td></tr><tr><th class="CalDayHeader" align="center" abbr="日" scope="col">日</th><th class="CalDayHeader" align="center" abbr="一" scope="col">一</th><th class="CalDayHeader" align="center" abbr="二" scope="col">二</th><th class="CalDayHeader" align="center" abbr="三" scope="col">三</th><th class="CalDayHeader" align="center" abbr="四" scope="col">四</th><th class="CalDayHeader" align="center" abbr="五" scope="col">五</th><th class="CalDayHeader" align="center" abbr="六" scope="col">六</th></tr><tr><td class="CalOtherMonthDay" align="center">28</td><td class="CalOtherMonthDay" align="center">29</td><td class="CalOtherMonthDay" align="center">30</td><td align="center">1</td><td align="center">2</td><td align="center">3</td><td class="CalWeekendDay" align="center">4</td></tr><tr><td class="CalWeekendDay" align="center">5</td><td class="CalTodayDay" align="center">6</td><td align="center">7</td><td align="center">8</td><td align="center">9</td><td align="center">10</td><td class="CalWeekendDay" align="center">11</td></tr><tr><td class="CalWeekendDay" align="center">12</td><td align="center">13</td><td align="center">14</td><td align="center">15</td><td align="center">16</td><td align="center">17</td><td class="CalWeekendDay" align="center">18</td></tr><tr><td class="CalWeekendDay" align="center">19</td><td align="center">20</td><td align="center">21</td><td align="center">22</td><td align="center">23</td><td align="center">24</td><td class="CalWeekendDay" align="center">25</td></tr><tr><td class="CalWeekendDay" align="center">26</td><td align="center">27</td><td align="center">28</td><td align="center">29</td><td align="center">30</td><td align="center">31</td><td class="CalOtherMonthDay" align="center">1</td></tr><tr><td class="CalOtherMonthDay" align="center">2</td><td class="CalOtherMonthDay" align="center">3</td><td class="CalOtherMonthDay" align="center">4</td><td class="CalOtherMonthDay" align="center">5</td><td class="CalOtherMonthDay" align="center">6</td><td class="CalOtherMonthDay" align="center">7</td><td class="CalOtherMonthDay" align="center">8</td></tr>
</tbody></table></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"><div id="sidebar_search" class="sidebar-block">
<div id="sidebar_search" class="mySearch">
<h3 class="catListTitle">搜索</h3>
<div id="sidebar_search_box">
<div id="widget_my_zzk" class="div_my_zzk"><input type="text" id="q" onkeydown="return zzk_go_enter(event);" class="input_my_zzk">&nbsp;<input onclick="zzk_go()" type="button" value="找找看" id="btnZzk" class="btn_my_zzk"></div>
<div id="widget_my_google" class="div_my_zzk"><input type="text" name="google_q" id="google_q" onkeydown="return google_go_enter(event)" class="input_my_zzk">&nbsp;<input onclick="google_go()" type="button" value="谷歌搜索" class="btn_my_zzk"></div>
</div>
</div>

</div><div id="sidebar_recentposts" class="sidebar-block">
<div class="catListEssay">
<h3 class="catListTitle">最新随笔</h3>
<ul>
<li><a href="http://www.cnblogs.com/dolphin0520/p/4040551.html">1. 金山快盘+TortoiseSVN构建版本控制仓库</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/4012804.html">2. 在windows下安装配置Ulipad</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3920385.html">3. Java并发编程：线程间协作的两种方式：wait、notify、notifyAll和Condition</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3932934.html">4. JAVA多线程和并发基础面试问答（转载）</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3958019.html">5. Java线程面试题 Top 50 （转载）</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3938991.html">6. Java并发编程：Timer和TimerTask（转载）</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3949310.html">7. Java并发编程：Callable、Future和FutureTask</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3920397.html">8. Java并发编程：CountDownLatch、CyclicBarrier和Semaphore</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3932921.html">9. Java并发编程：线程池的使用</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3932906.html">10. Java并发编程：阻塞队列</a></li>
</ul>
</div>
</div><div id="sidebar_categories">
<div id="sidebar_postcategory" class="catListPostCategory sidebar-block">
<h3 class="catListTitle">随笔分类<span style="font-size:11px;font-weight:normal">(193)</span></h3>

<ul>

<li><a id="CatList_LinkList_0_Link_0" href="http://www.cnblogs.com/dolphin0520/category/291783.html">Android(14)</a> </li>

<li><a id="CatList_LinkList_0_Link_1" href="http://www.cnblogs.com/dolphin0520/category/291771.html">C/C++(30)</a> </li>

<li><a id="CatList_LinkList_0_Link_2" href="http://www.cnblogs.com/dolphin0520/category/575043.html">J2EE(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_3" href="http://www.cnblogs.com/dolphin0520/category/602385.html">Java NIO(2)</a> </li>

<li><a id="CatList_LinkList_0_Link_4" href="http://www.cnblogs.com/dolphin0520/category/602384.html">Java并发编程(19)</a> </li>

<li><a id="CatList_LinkList_0_Link_5" href="http://www.cnblogs.com/dolphin0520/category/361055.html">Java基础(18)</a> </li>

<li><a id="CatList_LinkList_0_Link_6" href="http://www.cnblogs.com/dolphin0520/category/602390.html">Java集合框架</a> </li>

<li><a id="CatList_LinkList_0_Link_7" href="http://www.cnblogs.com/dolphin0520/category/602387.html">Java虚拟机</a> </li>

<li><a id="CatList_LinkList_0_Link_8" href="http://www.cnblogs.com/dolphin0520/category/480376.html">JS/CSS/Jquery(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_9" href="http://www.cnblogs.com/dolphin0520/category/361959.html">Linux/Shell(4)</a> </li>

<li><a id="CatList_LinkList_0_Link_10" href="http://www.cnblogs.com/dolphin0520/category/327629.html">OJ(14)</a> </li>

<li><a id="CatList_LinkList_0_Link_11" href="http://www.cnblogs.com/dolphin0520/category/400707.html">Python/Web框架(16)</a> </li>

<li><a id="CatList_LinkList_0_Link_12" href="http://www.cnblogs.com/dolphin0520/category/294641.html">XML(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_13" href="http://www.cnblogs.com/dolphin0520/category/470797.html">计算机网络</a> </li>

<li><a id="CatList_LinkList_0_Link_14" href="http://www.cnblogs.com/dolphin0520/category/417710.html">计算机系统(3)</a> </li>

<li><a id="CatList_LinkList_0_Link_15" href="http://www.cnblogs.com/dolphin0520/category/477333.html">开发工具(7)</a> </li>

<li><a id="CatList_LinkList_0_Link_16" href="http://www.cnblogs.com/dolphin0520/category/605219.html">设计模式(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_17" href="http://www.cnblogs.com/dolphin0520/category/309705.html">数据结构(11)</a> </li>

<li><a id="CatList_LinkList_0_Link_18" href="http://www.cnblogs.com/dolphin0520/category/407852.html">数据库(2)</a> </li>

<li><a id="CatList_LinkList_0_Link_19" href="http://www.cnblogs.com/dolphin0520/category/417089.html">数据挖掘(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_20" href="http://www.cnblogs.com/dolphin0520/category/291774.html">算法(27)</a> </li>

<li><a id="CatList_LinkList_0_Link_21" href="http://www.cnblogs.com/dolphin0520/category/471232.html">无线传感器网络(1)</a> </li>

<li><a id="CatList_LinkList_0_Link_22" href="http://www.cnblogs.com/dolphin0520/category/291786.html">信息检索</a> </li>

<li><a id="CatList_LinkList_0_Link_23" href="http://www.cnblogs.com/dolphin0520/category/442033.html">业余娱乐(6)</a> </li>

<li><a id="CatList_LinkList_0_Link_24" href="http://www.cnblogs.com/dolphin0520/category/470009.html">转载(14)</a> </li>

<li><a id="CatList_LinkList_0_Link_25" href="http://www.cnblogs.com/dolphin0520/category/417087.html">自然语言处理</a> </li>

</ul>

</div>

<div id="sidebar_links291873" class="catList sidebar-block">
<h3 class="catListTitle">常用链接</h3>

<ul>

<li><a id="CatList_LinkList_1_Link_0" href="http://www.cplusplus.com/reference/" rel="nofollow" target="_blank">C++ Reference</a> </li>

<li><a id="CatList_LinkList_1_Link_1" href="http://msdn.microsoft.com/zh-cn/default.aspx" rel="nofollow" target="_blank">MSDN 主页</a> </li>

<li><a id="CatList_LinkList_1_Link_2" href="http://cstest.scu.edu.cn/soj/index.action" rel="nofollow" target="_blank">SOJ</a> </li>

<li><a id="CatList_LinkList_1_Link_3" href="http://poj.org/" rel="nofollow" target="_blank">北大OJ</a> </li>

<li><a id="CatList_LinkList_1_Link_4" href="http://ifeve.com/" rel="nofollow" target="_blank">并发编程网</a> </li>

<li><a id="CatList_LinkList_1_Link_5" href="http://acm.hdu.edu.cn/" rel="nofollow" target="_blank">杭电OJ</a> </li>

</ul>

</div>

</div><div id="sidebar_scorerank" class="sidebar-block">
<div class="catListBlogRank">
<h3 class="catListTitle">积分与排名</h3>
<ul>
	<li class="liScore">
		积分 -	365121
	</li>
	<li class="liRank">
		排名 -	257
	</li>
</ul>
</div>


</div><div id="sidebar_recentcomments" class="sidebar-block"><div id="recent_comments_wrap">
<div class="catListComment">
<h3 class="catListTitle">最新评论</h3>

	<div id="RecentCommentsBlock"><ul>
    <li class="recent_comment_title"><a href="http://www.cnblogs.com/dolphin0520/p/3811445.html#3222012">1. Re:Java内部类详解</a></li>
    <li class="recent_comment_body">好文章，讲的很透彻，原理也进行了分析，谢谢楼主</li>
    <li class="recent_comment_author">--赤豆元宵</li>
    <li class="recent_comment_title"><a href="http://www.cnblogs.com/dolphin0520/p/3783345.html#3221901">2. Re:Java垃圾回收机制</a></li>
    <li class="recent_comment_body">可达性分析不就是和图差不多吗？我觉得按照图理解就可以。</li>
    <li class="recent_comment_author">--风吟无声</li>
    <li class="recent_comment_title"><a href="http://www.cnblogs.com/dolphin0520/p/3784171.html#3221667">3. Re:Java 如何有效地避免OOM：善于利用软引用和弱引用</a></li>
    <li class="recent_comment_body">赞</li>
    <li class="recent_comment_author">--Aby_</li>
</ul>
</div>
</div>
</div></div><div id="sidebar_topviewedposts" class="sidebar-block"><div id="topview_posts_wrap">
<div class="catListView">
<h3 class="catListTitle">阅读排行榜</h3>
	<div id="TopViewPostsBlock"><ul><li><a href="http://www.cnblogs.com/dolphin0520/archive/2013/04/29/3046237.html">1. 将Sublime Text 2搭建成一个好用的IDE(75258)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2011/08/26/2155202.html">2. Dijkstra算法（单源最短路径）(70815)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html">3. 堆排序(68731)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html">4. KMP算法(66581)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2011/08/25/2153720.html">5. 二叉树的非递归遍历(63939)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2013/01/17/2861164.html">6. 如何组装配置属于自己的台式机(35990)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2013/01/24/2843414.html">7. 当装系统时遇到“选中的磁盘采用GPT分区形式”(34144)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/archive/2011/12/02/2271922.html">8. Android中的"Unable to start activity ComponentInfo"或者"Unable to instantiate activity ComponentInfo"的错误(33264)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3355728.html">9. Android平台下利用zxing实现二维码开发(33025)</a></li><li><a href="http://www.cnblogs.com/dolphin0520/p/3383804.html">10. Android实现自定义带文字和图片的Button(32126)</a></li></ul></div>
</div>
</div></div></div><script type="text/javascript">loadBlogSideColumn();</script><script async="" type="text/javascript" src="./Java中的static关键字解析 - 海 子 - 博客园_files/check_359604.js"></script><iframe src="./Java中的static关键字解析 - 海 子 - 博客园_files/container.html" style="visibility: hidden; display: none;"></iframe>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		
<!--done-->
Copyright ©2015 海 子
	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->


<iframe id="google_osd_static_frame_2927144120912" name="google_osd_static_frame" style="display: none; width: 0px; height: 0px;"></iframe></body></html>